package com.bianmin.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bianmin.Model.Article;
import com.bianmin.Model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


@Mapper
public interface UserMapper extends BaseMapper<User> {

    //查询用户是否存在
    @Select("SELECT COUNT(*) > 0 FROM user WHERE open_id = #{openId}")
    boolean userExist(String openId);

    //根据微信openid查询用户信息
    @Select("select id, nick_name nickName, icon , create_time createTime , token  from user where open_id = #{openId}")
    Map<String,Object> queryUserWith(String openId);

    //根据用户id查询用户信息
    @Select("select * from user where id = #{userId}")
    User queryUserWithUserId(int userId);

    //根据手机号码查询用户信息
    @Select("select * from user where phone_number = #{phoneNumber}")
    User queryUserWithPhoneNumber(String phoneNumber);

    //根据token查询用户
    @Select("select * from user where token = #{token}")
    User queryUserWithToken(String token);

    //我发布的文章
    @Select("select t1.*, t2.icon as icon , t2.nick_name as nickName from article t1 join user t2 on t1.user_id = t2.id where t1.user_id = #{userId} order by create_time desc")
    List<Article> myArticle(String userId);

    //查询我的回复
    @Select("select t1.*, t2.title from comment as t1 join article as t2 on t1.article_id = t2.id\n" +
            "          where t1.user_id = #{userId} order by t1.id desc limit #{page} , #{pageSize}")
    List<Map<String,Object>> queryMyRepayWithUserId(String userId, int page , int pageSize);

    //查询收藏列表
    @Select("SELECT t1.*,t2.title FROM `collection` AS t1 JOIN article AS t2 ON t1.article_id = t2.id WHERE t1.user_id = #{userId} ORDER BY t1.id DESC LIMIT #{page} , #{pageSize}")
    List<Map<String,Object>> queryCollectionList(String userId, int page , int pageSize);

    //更新用户积分
    @Update("update user set quantity = #{quantity} where id = #{userId}")
    int updateUserQuantity(Integer quantity, Integer userId);

    //根据userId查询openId
    @Select("select open_id as openId from user where id = #{id}")
    String queryOpenIdById(@Param("id") String id);

}
